package com.test.burnincash.aaahpoppomelo.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.Order_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.UserData_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.view.EditItemView;
import com.test.burnincash.util_burnin.StringUtil_burnin;

import java.util.ArrayList;

public class AddOrderActivity_hpoppomelo extends BaseActivity_hpoppomelo {

    private EditText orderNumEt;
    private TextView dateTv;
    private EditText customerEt;

    private LinearLayout itemGroupLayout;

    private ArrayList<EditItemView> itemViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_order_hpoppomelo);
        init();
    }

    private void init(){
        setBackBtn_burnin(findViewById(R.id.activity_add_order_ib_back));

        orderNumEt = findViewById(R.id.activity_add_order_et_order_num);
        dateTv = findViewById(R.id.activity_add_order_tv_date);
        customerEt = findViewById(R.id.activity_add_order_et_customer);

        itemGroupLayout = findViewById(R.id.activity_add_order_layout_item_group);

        dateTv.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            View dialogView = getLayoutInflater().inflate(R.layout.dialog_date_picker_burnin,null);
            DatePicker datePicker = dialogView.findViewById(R.id.dialog_date_picker_view);
            datePicker.setCalendarViewShown(false);
            builder.setView(dialogView).setTitle(getResources().getString(R.string.dialog_date_picker_title));
            builder.setPositiveButton(getResources().getString(R.string.word_submit),(dialog,which) -> {
                int month = datePicker.getMonth() + 1;
                int day = datePicker.getDayOfMonth();
                int year = datePicker.getYear();
                String monthStr = month < 10 ? "0" + month : String.valueOf(month);
                String dayStr = day < 10 ? "0" + day : String.valueOf(day);
                String input = dayStr + "/" + monthStr + "/" + year;
                dateTv.setText(input);
                dialog.cancel();
            });
            builder.setNegativeButton(getResources().getString(R.string.word_cancel), ((dialog, which) -> dialog.cancel()));
            builder.show();
        });

        findViewById(R.id.activity_add_order_layout_add_item).setOnClickListener(v -> {
            @SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.item_edit_order_hpoppomelo,null);
            itemViews.add(new EditItemView(view));
            itemGroupLayout.addView(view);
        });

        findViewById(R.id.activity_add_order_btn_confirm).setOnClickListener(v -> {
            if (hasEtEmpty()){
                showToast_burnin(getString(R.string.word_input_tip));
                return;
            }

            int orderNum = StringUtil_burnin.safeParseInt(orderNumEt.getText().toString());

            if (UserData_hpoppomelo.getInstance().containOrderNum(orderNum)){
                showToast_burnin("The order number already exists: "+orderNum);
                return;
            }

            String date = StringUtil_burnin.getSafeString(dateTv.getText().toString());
            String customer = StringUtil_burnin.getSafeString(customerEt.getText().toString());

            Order_hpoppomelo order = new Order_hpoppomelo(orderNum,date,customer);

            for (EditItemView itemView:itemViews){
                order.addItem(itemView.getOrderItem());
            }

            UserData_hpoppomelo.getInstance().addOrder(order);
            if (!UserData_hpoppomelo.getInstance().getCustomers().contains(customer)) UserData_hpoppomelo.getInstance().addCustomer(customer);

            UserData_hpoppomelo.getInstance().uploadData();

            finish();

        });
    }

    private boolean hasEtEmpty(){
        if (TextUtils.isEmpty(orderNumEt.getText())||TextUtils.isEmpty(dateTv.getText())||TextUtils.isEmpty(customerEt.getText())) return true;
        for (EditItemView itemView:itemViews){
            if (itemView.hasEtEmpty()) return true;
        }
        return false;
    }


}